package com.example.administrator.daggerdemo.yujian;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.example.administrator.daggerdemo.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @项目名 Bot
 * @包名 org.auspeed.bot.adapter
 * @类名 CalendarAdapter
 * @时间 2016/11/20
 * @作者 YJ
 * @描述 TODO:日期控件填充器
 */
public class CalendarAdapter extends BaseAdapter {

    private List<Date> list;
    private Context context;
    private int selectPostion = -1;//默认选中项
    private int selectTodayPostion = -1;//默认今天

    public CalendarAdapter(List<Date> list, Context context) {
        this.context = context;
        this.list = list;
    }

    public void setSelectPostion(int position) {
        this.selectPostion = position;
    }

    public void setSelectTodayPostion(int position) {
        this.selectTodayPostion = position;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.holder_horizontal_calendar, null);
        }
        TextView tvCalendar = (TextView) convertView.findViewById(R.id.tv_calendar);
        TextView title = (TextView) convertView.findViewById(R.id.tv_calendar_title);

        SimpleDateFormat mSmpleDateFormat = new SimpleDateFormat("dd");
        tvCalendar.setText(mSmpleDateFormat.format(list.get(i)));
        String week = "";
        switch (i) {
            case 0:
                week = "一";
                break;
            case 1:
                week = "二";
                break;
            case 2:
                week = "三";
                break;
            case 3:
                week = "四";
                break;
            case 4:
                week = "五";
                break;
            case 5:
                week = "六";
                break;
            case 6:
                week = "日";
                break;
        }
        title.setText(week);

        //日期为今天
        if (selectTodayPostion == i) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if (simpleDateFormat.format(list.get(i)).equals(simpleDateFormat.format(new Date()))) {
                tvCalendar.setSelected(true);
                tvCalendar.setTextColor(Color.rgb(31, 204, 123));
                tvCalendar.setText("今");
                tvCalendar.setBackgroundResource(R.drawable.horizontal_calendar_bg);
            }
        }

        //日期为被选中
        if (selectPostion != -1) {
            if (selectPostion == i) {
                tvCalendar.setSelected(true);
                tvCalendar.setTextColor(Color.rgb(31, 204, 123));
                tvCalendar.setBackgroundResource(R.drawable.horizontal_calendar_bg);
            } else {
                tvCalendar.setSelected(false);
                tvCalendar.setTextColor(Color.rgb(102, 102, 102));
                tvCalendar.setBackgroundColor(Color.TRANSPARENT);
            }
        }
        return convertView;
    }
}
